// article JavaScript Document

$(function() {
	var isUnderIe6 = uaInfo.isIe && uaInfo.ieVr <= 6,
		isUnderIe7 = uaInfo.isIe && uaInfo.ieVr <= 7,
		isUnderIe8 = uaInfo.isIe && uaInfo.ieVr <= 8;

	/*******************************************************
	 *　tooltip
	 *******************************************************/
	$('.tooltip').powerTip({
		placement: isUnderIe6 ? 'e' : 'n'
	});
	$('.tooltip.zoom').each(function(i){
		$(this).data('powertiptarget', $(this).next().attr('id'));
	});


	/*******************************************************
	 * modal
	 *******************************************************/
	$('a[href=#modal_meyasu]').on('click', function(e){
		e.preventDefault();
		modalMeyasu.show();
	});
	var modalMeyasu = new APMN.Modal({
		target: $('#modal_meyasu'),
		close: $('#modal_meyasu .modal_close'),
		ieVr: !(isUnderIe6)
	});


	/*******************************************************
	 *　まとめてお問い合わせ
	 *******************************************************/
	var $aBtn = $('.mod_box_inquiry_all .button a'),
		SelectMax = 20;

	function changeInquiryBtn(b) {
		if (b) {
			$aBtn.removeClass('mod_btn_disabled');
		} else {
			$aBtn.addClass('mod_btn_disabled');
		};
	};

	$aBtn.on('click', function(e){
		if (isSelected) {
			var _num = 0;
			$('.type_list_wrap .mod_check').each(function(i){
				if ($(this).find('input').prop('checked')) {
					_num ++;
				};
			});
			if (SelectMax < _num) {
				e.preventDefault();
				modalOverSelect.show();
			};
		} else {
			e.preventDefault();
		};
	});

	var modalOverSelect = new APMN.Modal({
		target: $('#modal_gomen'),
		close: $('#modal_gomen .modal_close'),
		ieVr: !(isUnderIe6)
	});


	/*******************************************************
	 *　Inputボタン
	 *******************************************************/
	var isSelected = null,
		selectDispath = false,
		$selectAll = $('.mod_box_inquiry_all'),
		$selectOne = $('.type_list_wrap');

	if (!isUnderIe8) {
		$selectAll.find('input')
			.on('ifChecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				$selectOne.find('input').iCheck('check');
				changeCheckBox();
				selectDispath = false;
			})
			.on('ifUnchecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				$selectOne.find('input').iCheck('uncheck');
				changeCheckBox();
				selectDispath = false;
			});
		$selectOne.find('input')
			.on('ifChecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				changeCheckBox();
				selectDispath = false;
			})
			.on('ifUnchecked', function(e){
				if (selectDispath) return;
				selectDispath = true;
				if ($selectAll.find('.mod_check').hasClass('mod_checked')) {
					$selectAll.find('input').iCheck('uncheck');
				};
				changeCheckBox();
				selectDispath = false;
			});
	} else {
		$selectAll.find('input')
			.on('click', function(e){
				var b = $(this).prop('checked');
				$selectOne.find('input').prop('checked', b);
				changeCheckBox();
			});
		$selectOne.find('li input')
			.on('click', function(e){
				var b = $(this).prop('checked');
				if (!b) {
					$selectAll.find('input').prop('checked', false);
				}
				changeCheckBox();
			});
	}

	function changeCheckBox(){
		var _isSlt = false;
		$('.mod_check').each(function(e){
			if ($(this).find('input').prop('checked')) {
				_isSlt = true;
				return false;
			};
		});
		if (isSelected != _isSlt) {
			isSelected = _isSlt;
			if (isSelected) {
				changeInquiryBtn(true);
			} else {
				changeInquiryBtn(false);
			}
		};
	};
	changeCheckBox();


	/*******************************************************
	 * 検証リストに追加
	 *******************************************************/
	$('.type_list_wrap .mod_table').find('a.mod_icn_star').on('click', function(e){
		e.preventDefault();
		if (!$(this).hasClass('mod_icn_star_active')) {
			$(this).addClass('mod_icn_star_active');
			addStar.show();
		};
	});

	$('.btn_add a.mod_btn_s').on('click', function(e){
		e.preventDefault();
		if (!$(this).hasClass('mod_btn_active')) {
			$('.btn_add a.mod_btn_s').addClass('mod_btn_active');
			addStar.show();
		};
	});

	var addStar = new APMN.ModalStar({
		path: '/static/shared/images/img_add_star.png',
		ieVr: !(isUnderIe6)
	});


	/*******************************************************
	 * スライダー img_slider
	 *******************************************************/
	var slide1ControlNum = APMN.SliderRemake({
		num: 6,
		target: $('.img_slider'),
		ieVr: !(isUnderIe7)
	});

	var imgSlider1 = $('.img_slider .bxslider').bxSlider({
		pager: false,
		controls: slide1ControlNum > 1,
		nextSelector: '.img_slider .btn_next',
		prevSelector: '.img_slider .btn_prev'
	});

	$('.img_slider ul li').each(function(i){
		var _id = parseInt($(this).attr('class').substr(3), 10);
		$(this).find('a').on('click', function(e){
			e.preventDefault();
			modalGallery.show();
			modalSlider.reloadSlider();
			changeGallery(_id);
		});
	});


	/*******************************************************
	 * modal ギャラリー
	 *******************************************************/
	var glrNow = 0,
		glrMax = $('.gallery_thumb li').length,
		glrList = [];

	$('.gallery_thumb li').each(function(i){
		glrList.push({
			src : $(this).find('img').attr('src'),
			txt : $(this).find('p').text()
		});
	});

	var modalControlNum = APMN.SliderRemake({
		num: 9,
		target: $('.gallery_thumb'),
		ieVr: !(isUnderIe7)
	});

	var modalSlider = $('.gallery_thumb .bxslider').bxSlider({
		pager: false,
		controls: false,
		onSlideBefore: CallFuncSlide
	});

	var modalGallery = new APMN.Modal({
		target: $('#modal_gallery'),
		close: $('#modal_gallery .modal_close'),
		ieVr: !(isUnderIe6)
	});

	function CallFuncSlide(e, o, n){
		$('.gallery_nav li').each(function(i){
			$(this).removeClass('on');
			if (n + 1 == i) {
				$(this).addClass('on');
			};
		});
	};

	function changeGallery(n){
		if(glrList == undefined) return;
		if (n == glrNow) {
			return;
		};
		var _pre = glrNow;
		glrNow = n;
		if(glrNow < 1) return;
		$('.gallery_main span img').attr('src', glrList[glrNow - 1].src);
		$('.gallery_main p').text(glrList[glrNow - 1].txt);
		var _nowc = Math.ceil(_pre / 9),
			_nxtc = Math.ceil(glrNow / 9);
		if (_nowc != _nxtc || modalSlider.getCurrentSlide() + 1 != _nxtc) {
			modalSlider.goToSlide(_nxtc - 1);
		};
		$('.gallery_thumb ul li').each(function(i){
			$(this).removeClass('on');
			var _id = parseInt($(this).attr('class').substr(3), 10);
			if (_id == glrNow) {
				$(this).addClass('on');
			}
		});
	};

	function setGallery(){
		if (modalControlNum == 1) {
			$('.gallery_nav').hide();
		};

		$('.gallery_thumb ul:not(.gallery_nav) li').each(function(i){
			var _id = parseInt($(this).attr('class').substr(3), 10);
			$(this).find('a').on('click', function(e){
				e.preventDefault();
				changeGallery(_id)
			});
		});

		$('.gallery_main .btn_next').on('click', function(e){
			e.preventDefault();
			var _n = glrNow + 1 > glrMax ? 1 : glrNow + 1;
			changeGallery(_n);
		});
		$('.gallery_main .btn_prev').on('click', function(e){
			e.preventDefault();
			var _n = glrNow - 1 < 1 ? glrMax : glrNow - 1;
			changeGallery(_n);
		});

		$('.gallery_nav li').each(function(i){
			if ($(this).hasClass('nav_prev')) {
				$(this).on('click', function(e){
					modalSlider.goToPrevSlide();
				});
			}
			else if ($(this).hasClass('nav_next')) {
				$(this).on('click', function(e){
					modalSlider.goToNextSlide();
				});
			}
			else {
				$(this).on('click', function(e){
					modalSlider.goToSlide(i - 1);
				});
			};
		});
	};
	setGallery();
	changeGallery(1);


	/*******************************************************
	 * スライダー img_slider2
	 *******************************************************/
	var slide2ControlNum = APMN.SliderRemake({
		num: 5,
		target: $('.img_slider2'),
		ieVr: !(isUnderIe7)
	});

	var imgSlider2 = $('.img_slider2 .bxslider').bxSlider({
		pager: false,
		controls: slide2ControlNum > 1,
		nextSelector: '.img_slider2 .btn_next',
		prevSelector: '.img_slider2 .btn_prev'
	});


	/*******************************************************
	 * map
	 *******************************************************/
	var $mapOpnBtn = $('.around_map .open_menu'),
		$mapOpnTrg = $('.around_map .map');
	var mapOpener = new APMN.ToggleOpener({
		btn: $mapOpnBtn,
		target: $mapOpnTrg,
		isOpen: $('.around_map .open_menu').hasClass('active')
	});
	mapOpener.toggle = function(b) {
		if (b) $mapOpnBtn.addClass('active');
		else $mapOpnBtn.removeClass('active');
	};

	function makeMap() {
		var x = parseFloat($('.map').attr('lat'))*3600;
		var y = parseFloat($('.map').attr('lon'))*3600;
		var scl = 200;

		var map = new Mapple.SingleMap("map", 940, 400, x, y, scl, {
			k1: "apanew",
			k2: "1205jdwa"
		});

		var iconLayer = map.createOverlayIcons();
		iconLayer.append("icon0", x, y, {
			name: $(".box_head_tab_room .name").text(),
			normalSrc: "/static/shared/images/icon/map/icon_home.png",
			clickMove: true,
			cursor: "pointer",
			scaleLevel: null,
			state: 'normal',
			normal: {
				leftOffset: -20,
				leftOffsetUnit: 'pixels',
				topOffset: 60,
				topOffsetUnit: 'pixels',
				scale: 1.0
			}
		});
	};
	makeMap();


	/*******************************************************
	 * heightLine
	 *******************************************************/
	$(".article_index .around_article li").heightLine();
	$(".img_gallery>div>span").heightLine();
	$(".shop_info .heightLine").heightLine();
	$(".article_dtl .article_recommend .img_slider2 ul li").heightLine();
	$(".article_dtl .article_recommend .img_slider2 ul li>span").heightLine();
	$(".article_dtl .article_recommend .img_slider2 ul li p.cell01").heightLine();
});
